package com.leetcode.test.linklist.classic;


import com.leetcode.test.linklist.ListNode;

import org.junit.Test;

/**
 * https://leetcode-cn.com/explore/learn/card/linked-list/195/classic-problems/753/
 * <p>
 * 解题：https://leetcode-cn.com/problems/odd-even-linked-list/solution/qi-ou-lian-biao-by-leetcode/
 */

public class 奇偶链表 {

	@Test
	public void test() {
//		输入: 1->2->3->4->5->NULL
//		输出: 1->3->5->2->4->NULL
		ListNode node1 = new ListNode(1);
		ListNode node2 = node1.next = new ListNode(2);
		ListNode node3 = node2.next = new ListNode(3);
		ListNode node4 = node3.next = new ListNode(4);
		ListNode node5 = node4.next = new ListNode(5);

		ListNode newList = oddEvenList(node1);

		System.out.println();
	}

	public ListNode oddEvenList(ListNode head) {
		if (head == null) return null;
		ListNode odd = head, even = head.next, evenHead = even;
		while (even != null && even.next != null) {
			odd.next = even.next;
			odd = odd.next;
			even.next = odd.next;
			even = even.next;
		}
		odd.next = evenHead;
		return head;
	}
}
